//
//  ChatNoticeView.m
//  Btpin
//
//  Created by Vic on 2019/4/28.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "ChatNoticeView.h"

@interface ChatNoticeView ()
{
    UILabel *titleLbl;
    UILabel *contentLbl;
    UILabel *actionLbl;
}
@end

@implementation ChatNoticeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        self.viewHeight = 0;
    }
    return self;
}
- (void)initUI {
    self.backgroundColor = COLOR_OF_WHITE;
    titleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(20*Scale, 10*Scale, self.frame.size.width-40*Scale, 30*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
    [self addSubview:titleLbl];
    
    contentLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(titleLbl), EndY(titleLbl), ViewWidth(titleLbl), 30*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_666666 font:Font_12];
    contentLbl.numberOfLines = 0;
    contentLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [self addSubview:contentLbl];
    
    actionLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(titleLbl), EndY(contentLbl)+10*Scale, ViewWidth(titleLbl), 40*Scale) text:nil textColor:THEMECOLOR_OF_ORANGE font:Font_15];
    actionLbl.hidden = YES;
    [self addSubview:actionLbl];
    [actionLbl borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    
    UIImageView *imgView = [UIToolsControl initImageViewWithFrame:CGRectMake(ViewWidth(titleLbl)-26*Scale, 13*Scale, 7*Scale, 13*Scale) imageName:@"jiantou_right"];
    imgView.userInteractionEnabled = NO;
    [actionLbl addSubview:imgView];
}
- (void)reloadDataForModel:(IMCustomModel *)model {
    titleLbl.text = model.title;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:LineSpacingHeight*2];
    NSInteger textHeight = ceil([model.content boundingRectWithSize:CGSizeMake(ViewWidth(titleLbl), MAXFLOAT) options:(NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:Font(Font_12), NSParagraphStyleAttributeName:paragraphStyle} context:nil].size.height)+1;
    contentLbl.frame = CGRectMake(StartX(titleLbl), EndY(titleLbl), ViewWidth(titleLbl), textHeight);
    contentLbl.attributedText = [[NSAttributedString alloc] initWithString:model.content attributes:@{NSFontAttributeName:Font(Font_12), NSParagraphStyleAttributeName:paragraphStyle}];
    
    self.viewHeight = EndY(contentLbl)+10*Scale;
    
    if (model.actions.count) {
        actionLbl.hidden = NO;
        actionLbl.frame = CGRectMake(StartX(titleLbl), EndY(contentLbl)+10*Scale, ViewWidth(titleLbl), 40*Scale);
        NSDictionary *dic = model.actions[0];
        actionLbl.text = dic[@"name"];
        self.viewHeight = EndY(actionLbl);
    } else {
        actionLbl.hidden = YES;
    }
    
    CGRect frame = self.frame;
    frame.size.height = self.viewHeight;
    self.frame = frame;
}

@end
